package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.ShopmsShopParam;
import com.macro.mall.model.PmsShop;
import com.macro.mall.service.PmsShopService;
import com.macro.mall.util.HttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

import java.util.List;


/**
 * 店铺信息表(PmsShop)表控制层
 *
 * @author liu
 * @since 2021-04-21
 */
@RestController
@Api(tags = "pmsShopController", description = "店铺管理")
@RequestMapping("pmsShop")
public class PmsShopController {

    public static final String APP_KEY = "2aa1ebb841bac617";// 你的appkey
    public static final String URL = "https://api.jisuapi.com/area/query";

    /**
     * 服务对象
     */
    @Resource
    private PmsShopService pmsShopService;

    @ApiOperation("获取地域信息")
    @GetMapping("area/{parentId}")
    public CommonResult getAreaInfo(@PathVariable int parentId) {
        String result = null;
        String url = URL + "?parentid=" + parentId + "&appkey=" + APP_KEY;
        try {
            result = HttpUtil.sendGet(url, "utf-8");
            JSONObject json = JSONObject.fromObject(result);
            if (json.getInt("status") != 0) {
                System.out.println(json.getString("msg"));
            } else {
                JSONArray resultarr = json.optJSONArray("result");
                for (int i = 0; i < resultarr.size(); i++) {
                    JSONObject obj = (JSONObject) resultarr.opt(i);
                    String id = obj.getString("id");
                    String name = obj.getString("name");
                    String parentname = obj.getString("parentname");
                    String areacode = obj.getString("areacode");
                    String zipcode = obj.getString("zipcode");
                    String depth = obj.getString("depth");
                    System.out.println(id + " " + name + " " + parentname + " " + areacode + " " + zipcode + " " + depth);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.success(result);
    }

    /**
     * 根据主键id查询单个店铺信息
     *
     * @param id 主键
     * @return 实例对象
     */
    @ApiOperation("根据店铺id查询店铺详细信息")
    @GetMapping("selectOne/{id}")
    public CommonResult selectOne(@PathVariable Long id) {
        System.out.println("id => " + id);
        System.out.println("shop => ");
        System.out.print(this.pmsShopService.queryById(id));
        return CommonResult.success(this.pmsShopService.queryById(id));
    }

    /**
     * 分页查询所有店铺信息
     *
     * @param pageNum 当前第几页
     * @param pageSize 一页有多少记录
     * @return 所有店铺信息list
     */
    @ApiOperation("查询所有店铺")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmsShop>> selectAll(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        List<PmsShop> pmsShopList = pmsShopService.selectAll(pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(pmsShopList));
    }

    /**
     * 新增店铺
     *
     * @param pmsShop 店铺对象
     * @return 成功与否
     */
    @ApiOperation("新增店铺")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult insert(@Validated @RequestBody ShopmsShopParam param) {
        int count = pmsShopService.insert(param);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改店铺信息
     *
     * @param pmsShop 店铺对象
     * @return 成功与否
     */
    @ApiOperation("修改店铺所有信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@Validated @RequestBody PmsShop pmsShop) {
//        pmsShop.setId(id);
        int count = pmsShopService.update(pmsShop);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除店铺（通过主键id）
     *
     * @param id 主键id
     * @return 成功与否
     */
    @ApiOperation("删除店铺")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        boolean count = pmsShopService.deleteById(id);
        if (count) {
            return CommonResult.success(true);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改店铺状态
     *
     * @param id 主键id
     * @param status 店铺状态0->不显示；1->显示
     * @return 成功与否
     */
    @ApiOperation("修改店铺状态")
    @RequestMapping(value = "/updateStatus/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateStatus(@PathVariable Long id,@RequestParam(value ="status") Integer status) {
        PmsShop pmsShop= new PmsShop();
        pmsShop.setId(id);
        pmsShop.setShowStatus(status);
        int count = pmsShopService.update(pmsShop);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 批量删除店铺（店铺id）
     *
     * @param shopIds 店铺id list
     * @return 成功与否
     */
    @ApiOperation("批量删除店铺")
    @RequestMapping(value = "/deleteShops", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteShops(@RequestParam List<Long> shopIds) {
        int count = pmsShopService.deleteShops(shopIds);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 组合查询
     *
     * @param pageNum 当前第几页
     * @param pageSize 一页有多少记录
     * @param shopName 店铺名字模糊查询
     * @param status 店铺状态0->不显示；1->显示
     * @return 查询结果列表
     */
    @ApiOperation("根据店铺名字和状态组合模糊查询")
    @GetMapping("selectByNameAndStatus")
    @ResponseBody
    public CommonResult<CommonPage<PmsShop>> selectByNameAndStatus(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(value = "keyword", required = false) String shopName,
                                                                   @RequestParam(value = "status", required = false) String status){
        int showStatus=0;
        if(status.isEmpty()){//判断状态是否为空，因为int无法判断所以接受string类型
            showStatus=-1;
        }else{
            showStatus=Integer.parseInt(status);//判断非空后转换成int类型
        }
        List<PmsShop> pmsShopList = pmsShopService.selectByNameAndStatus(pageSize, pageNum,shopName,showStatus);
        return CommonResult.success(CommonPage.restPage(pmsShopList));
    }
}
